package com.iyimo.control
{
	import com.iyimo.i.ITimeLogic;
	import com.iyimo.util.TimeMachine;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class CDButton extends Button implements ITimeLogic{
		/**
		 * 遮罩
		 */
		public var maskSprite:MovieClip;
		
		/**
		 * 冷却时间,ms
		 */		
		public var cdTime:int;
		
		/**
		 * 冷却剩余的时间
		 */		
		private var _cdTimeRest:int = 0;
		
		/**
		 * 是否冷却中
		 */
		private var _isCD:Boolean;
		
		public function CDButton(){}

		/**
		 * 带有冷却的btn,点击之后,一段时间不能点击
		 * @param style
		 * @return 
		 */		
		public static function createCDBtn(style:MovieClip, maskSprite:MovieClip, cdTime:int):CDButton{
			var btn:CDButton = new CDButton();
			btn.style = style;
			maskSprite.y = style.height - maskSprite.height;
			btn.maskSprite = maskSprite;
			btn.cdTime = cdTime;
			return btn;
		}

		/**
		 * 是否冷却中
		 * true 开始冷却
		 * false 结束冷却
		 */
		public function get isCD():Boolean{
			return _isCD;
		}
		
		public function set isCD(value:Boolean):void{
			_isCD = value;
			if (_isCD){
				_cdTimeRest = cdTime;
				_style.addChild(maskSprite);
				(maskSprite["mask_mc"] as MovieClip).scaleY = 1;
			}else {
				_style.removeChild(maskSprite);
				(maskSprite["mask_mc"] as MovieClip).scaleY = 0;
			}
		}
		
		public function onTimer():void{
			_cdTimeRest--;
			maskSprite["mask_mc"].scaleY = _cdTimeRest / cdTime;
			if (_cdTimeRest <= 0){
				TimeMachine.endTimer(this);
				isCD = false;
			}
		}
		
		override protected function onOver(e:MouseEvent):void{
			if (_isCD)return;
			super.onOver(e);
		}
		
		override protected function onOut(e:MouseEvent):void{
			if (_isCD)return;
			super.onOut(e);
		}
		
		override protected function onDown(e:MouseEvent):void{
			if (_isCD)return;
			super.onDown(e);
		}
		
		override protected function onUp(e:MouseEvent):void{
			if (_isCD)return;
			super.onUp(e);
			isCD = true;
			TimeMachine.startTimer(this);
		}
	}
}